/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */package com.ruipengkj.component.tabbedPane;

import java.awt.Component;

import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.ruipengkj.commons.util.Util;
import com.ruipengkj.factory.ImageFactory;
/**
 * 说明：
 * 重写JTabbedPane
 * @Author：李智深
 * @Date：2012-07-04
 */
public class RPTabbedPane extends JTabbedPane {

	private static final long serialVersionUID = 1L;
	private int preferredUnselectedTabWidth = 80;
    private int preferredTabHeight = ImageFactory.loadImage(ImageFactory.TabbedPane_header_background).getIconHeight();

    public RPTabbedPane() {
        init();
    }

    private void init() {
    	this.setTabPlacement(JTabbedPane.BOTTOM);
        this.setFont(Util.FONT_12_BOLD);
        this.setForeground(Util.DEFAULT_TEXT_COLOR);
        this.setBackground(Util.CONTENT_PANE_BACKGROUND);
        this.setBorder(null);
        this.setFocusable(false);
        this.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        this.setOpaque(false);
        this.setUI(new RPTabbedPaneUI(this));
        this.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                //inform each tab component that select changed.
                updateTabComponents();
            }
        });
    }

    @Override
    public void addTab(String title, Component component) {
        super.addTab(title, component);
        int index = this.getTabCount() - 1;
        RPTabComponent tabComponent = new RPTabComponent(this);
        tabComponent.setTitle(title);
        this.setTabComponentAt(index, tabComponent);
        this.setToolTipTextAt(index, title);
        updateTabComponents();
    }

    public int getPreferredTabHeight() {
        return preferredTabHeight;
    }

    private void updateTabComponents() {
        int selectedIndex = this.getSelectedIndex();
        for (int i = 0; i < this.getTabCount(); i++) {
            Component c = this.getTabComponentAt(i);
            if (c instanceof RPTabComponent) {
                RPTabComponent component = (RPTabComponent) c;
                boolean selected = selectedIndex == i;
                component.updateSelection(selected);
            }
        }
    }

    public int getPreferredUnselectedTabWidth() {
        return preferredUnselectedTabWidth;
    }
}
